package com.lcj.crawler;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.lcj.model.CompanyRecruitInfo;
import com.lcj.service.CompanyRecruitInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SaveCompanyRecruit implements Pipeline<NextPageZzz4> {

    @Resource(name="companyRecruitInfoServiceImpl")
    private CompanyRecruitInfoService companyRecruitInfoService;
    @Override
    public void process(NextPageZzz4 nextPageZzz4) {
        // 保存当前页爬取的职位信息

        List<CompanyRecruit> list = nextPageZzz4.getCompanyRecruits();
        for(CompanyRecruit companyRecruit:list){
            CompanyRecruitInfo companyRecruitInfo = new CompanyRecruitInfo();
            companyRecruitInfo.setCompanyName(companyRecruit.getCompanyName());
            companyRecruitInfo.setIndustryName(companyRecruit.getIndustryName());
            companyRecruitInfo.setLowerEdu(companyRecruit.getLowerEdu());
            companyRecruitInfo.setPersons(companyRecruit.getPersons());
            companyRecruitInfo.setRecruitName(companyRecruit.getRecruitName());
            companyRecruitInfo.setRecruitUrl(companyRecruit.getRecruitUrl());
            companyRecruitInfo.setSalary(companyRecruit.getSalary());
            companyRecruitInfo.setWelfare(companyRecruit.getWelfare());
            companyRecruitInfo.setWorkPlace(companyRecruit.getWorkPlace());
            if(companyRecruit.getRecruitDate()!=null&&!companyRecruit.getRecruitDate().equals("")){
                companyRecruitInfo.setRecruitDate(companyRecruit.getRecruitDate());
            }else{
                companyRecruitInfo.setRecruitDate("近期");
            }
            companyRecruitInfo.setSource("株洲在线人才网");
            companyRecruitInfoService.save(companyRecruitInfo);

            //继续抓取联系方式详细信息
//          String ajaxUrl = "http://job.hnw.net/plus/ajax_contact.php?id=6875&time=1536744658949&act=jobs_contact";
//          String recruitId = companyRecruit.getRecruitUrl().split("=")[1];
            String recruitId =StringUtils.substringAfter(companyRecruit.getRecruitUrl(),"id=");
            long ajaxTime = System.currentTimeMillis();
            String ajaxUrl = "http://job.hnw.net/plus/ajax_contact.php?id="+recruitId+"&time="+ajaxTime+"&act=jobs_contact";
            HttpRequest sub = nextPageZzz4.getRequest().subRequest(ajaxUrl);
            DeriveSchedulerContext.into(sub);
        }

        //获取当前页和最后页，循环爬取全部页面
        int currentPage = nextPageZzz4.getCurrentPage();
        String lastPageUrl = nextPageZzz4.getLastPageUrl();
        int totalPage = getTotalPage(lastPageUrl);
        if (currentPage < totalPage){
            int nextPage = currentPage+1;
            String nextUrl = "http://job.hnw.net/jobs/jobs-list.php?page="+nextPage;
            HttpRequest request = nextPageZzz4.getRequest().subRequest(nextUrl);
            DeriveSchedulerContext.into(request);
        }
    }

    private int getTotalPage(String lastPageUrl) {
        if(StringUtils.isEmpty(lastPageUrl)) {
            return 1;
        }
        //http://job.hnw.net/jobs/jobs-list.php?page=1
        lastPageUrl = StringUtils.substringAfter(lastPageUrl, "=");
        return NumberUtils.toInt(lastPageUrl, 1);
    }
}
